package com.yazi.mxz.admin.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import com.myframework.smhj.common.exception.MyException;
import com.myframework.smhj.common.tool.PageLink;
import com.myframework.smhj.hibernate.Searching;
import com.myframework.smhj.hibernate.Page;
import com.myframework.smhj.hibernate.Sort;
import com.myframework.smhj.jackson.result.StatusResult;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yazi.mxz.common.controller.BaseController;
import com.yazi.mxz.common.entity.beans.Prize;
import com.yazi.mxz.common.entity.enums.PrizeStatus;
import com.yazi.mxz.common.entity.enums.PrizeType;
import com.yazi.mxz.common.service.PrizeService;

@Controller("prizeController")
@RequestMapping("/prize")
public class PrizeController extends BaseController {
	private static Logger log = LoggerFactory.getLogger(PrizeController.class);
	
	@Resource
	PrizeService prizeService;
	
	@RequestMapping("/list")
	public String list(Prize prize, Searching si, Model model) {
		log.debug("进入奖品列表页面");
		List<Sort> sorts = new ArrayList<Sort>();
		sorts.add(new Sort("prizeId"));
		si.setSorts(sorts);
		Page<Prize> page = prizeService.list(prize, si);
		PageLink pageLink = new MyPageLink(page.getPaging());
		// 奖品类型枚举
		PrizeType[] types = PrizeType.values();
		// 上架状态枚举
		PrizeStatus[] statuses = PrizeStatus.values();
		
		model.addAttribute("prizeList", page.getList());
		model.addAttribute("pageLink", pageLink);
		model.addAttribute("searchWord", si.getSearchWord());
		model.addAttribute("prizeTypes", types);
		model.addAttribute("prizeStatuses", statuses);
		return "prize_list";
	}
	
	@RequestMapping("/addDo/json")
	@ResponseBody
	public StatusResult addDoJson(Prize prize, Model model) {
		log.debug("添加奖品");
		StatusResult jsonResult = new StatusResult();
		try{
			// 初始化奖品
			prize.setName("奖品名称");
			prize.setStatus(PrizeStatus.SOLD_OUT.getValue()); // 默认下架
			prize.setSummary("奖品描述");
			
			prizeService.add(prize);
			
			jsonResult.setOk();
		} catch (MyException e) {
			log.debug("添加奖品失败: {}", e.getMessage());
			jsonResult.setFailMessage(e.getMessage());
		} catch (Exception e) {
			log.error("添加奖品异常", e);
			jsonResult.setError();
		}
		
		return jsonResult;
	}
	
	@RequestMapping("/updateDo/json")
	@ResponseBody
	public StatusResult updateDoJson(Prize prize) {
		log.debug("修改奖品:prizeId={}", prize.getPrizeId());
		StatusResult jsonResult = new StatusResult();
		try{
			prizeService.edit(prize);
			jsonResult.setOk();
		} catch (MyException e) {
			log.debug("修改奖品失败: {}", e.getMessage());
			jsonResult.setFailMessage(e.getMessage());
		} catch (Exception e) {
			log.error("修改奖品异常:", e);
			jsonResult.setError();
		}
		
		return jsonResult;
	}
	
	@RequestMapping("/removeDo/json")
	@ResponseBody
	public StatusResult removeDoJson(Long prizeId) {
		log.debug("删除奖品:prizeId={}", prizeId);
		StatusResult jsonResult = new StatusResult();
		try{
			prizeService.delete(prizeId);
			jsonResult.setOk();
		} catch (Exception e) {
			log.error("删除奖品异常 ", e);
			jsonResult.setError();
		}
		return jsonResult;
	}
}
